﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for TinTucCtr
/// </summary>
public class TinTucCtr
{
	public TinTucCtr()
	{
		
	}
    public DataTable layTinTuc()
    {
        try
        {
            string sql = "select * from TinTuc Where TrangThai='True'";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public DataTable layTinTucTheoMa(int matt)
    {
        try
        {
            string sql = "select * from TinTuc where matt=@matt and TrangThai='True'";
            return DataProvider.GetData(sql,"@matt",matt);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public ETinTuc GetTinTuc(string MaTT)
    {
        try
        {
            DataTable tableTinTuc = DataProvider.GetData("SELECT * FROM TinTuc WHERE MaTT=@MaTT and TrangThai='True'", "@MaTT", MaTT);
            ETinTuc tinTuc = new ETinTuc();
            tinTuc.MaTT = Int32.Parse(tableTinTuc.Rows[0]["MaTT"].ToString());
            tinTuc.TieuDe = tableTinTuc.Rows[0]["TieuDe"].ToString();
            tinTuc.MoDau = tableTinTuc.Rows[0]["MoDau"].ToString();
            tinTuc.Noidung = tableTinTuc.Rows[0]["Noidung"].ToString();
            tinTuc.HinhAnh = tableTinTuc.Rows[0]["HinhAnh"].ToString();
            tinTuc.LoaiTin = Int32.Parse(tableTinTuc.Rows[0]["LoaiTin"].ToString());
            tinTuc.TrangThai = tableTinTuc.Rows[0]["TrangThai"].ToString();
            tinTuc.Quantri = tableTinTuc.Rows[0]["Quantri"].ToString();
           
            return tinTuc;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    //Hanh
    public DataTable layTinTucH()
    {
        try
        {
            string sql = "select TinTuc.*,LoaiTinTuc.TenLoai from TinTuc,LoaiTinTuc where TinTuc.LoaiTT=LoaiTinTuc.MaLoaiTT and TrangThai='True'";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public DataTable layLoaiTinTuc()
    {
        try
        {
            string sql = "select * from LoaiTinTuc";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public ETinTuc laytinTucH(string MaTT)
    {
        try
        {
            DataTable tableTinTuc = DataProvider.GetData("Select * from TinTuc Where MaTT=@MaTT", "@MaTT", MaTT);
            ETinTuc TinTuc = new ETinTuc();
            TinTuc.MaTT = Int32.Parse(tableTinTuc.Rows[0]["MaTT"].ToString());
            TinTuc.TieuDe = tableTinTuc.Rows[0]["TieuDe"].ToString();
            TinTuc.MoDau = tableTinTuc.Rows[0]["MoDau"].ToString();
            TinTuc.LoaiTin = Int32.Parse(tableTinTuc.Rows[0]["LoaiTT"].ToString());
            TinTuc.HinhAnh = tableTinTuc.Rows[0]["Hinhanh"].ToString();
            TinTuc.Noidung = tableTinTuc.Rows[0]["NoiDung"].ToString();
            TinTuc.TrangThai = tableTinTuc.Rows[0]["TrangThai"].ToString();
            TinTuc.Quantri = tableTinTuc.Rows[0]["QuanTri"].ToString();
            return TinTuc;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool InsertTinTucH(ETinTuc TinTuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO TinTuc VAlUES(@LoaiTin,@QuanTri,@TieuDe,@MoDau,@NoiDung,@Hinhanh,@Tinhtrang)",
                "@LoaiTin", TinTuc.LoaiTin,"@QuanTri", TinTuc.Quantri, "@TieuDe", TinTuc.TieuDe,"@MoDau",TinTuc.MoDau,"@NoiDung", TinTuc.Noidung, "@Hinhanh", TinTuc.HinhAnh, "@Tinhtrang", TinTuc.TrangThai);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
    public bool UpdateTinTucH(ETinTuc TinTuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update TinTuc set TieuDe=@TieuDe,MoDau=@MoDau,LoaiTT=@LoaiTin,Hinhanh=@Hinhanh,NoiDung=@NoiDung,TrangThai=@Tinhtrang WHERE MaTT=@MaTT", "@MaTT", TinTuc.MaTT, "@TieuDe", TinTuc.TieuDe,"@MoDau",TinTuc.MoDau, "@Hinhanh", TinTuc.HinhAnh, "@NoiDung", TinTuc.Noidung, "@Tinhtrang", TinTuc.TrangThai, "@LoaiTin", TinTuc.LoaiTin);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
    public bool DeleteTinTucH(string MaTT)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM TinTuc WHERE MaTT=@MaTT", "@MaTT", MaTT);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
    public bool DeleteTinTucH1(string MaTT)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update TinTuc set TrangThai=0 WHERE MaTT=@MaTT", "@MaTT", MaTT);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
}